﻿using System;
using System.IO;
using NeuralStock.DataAccess;
using NeuralStock.DomainService.Interfaces;
using NeuralStock.Entities;
using NeuralStock.Common;
using System.Collections.Generic;
using NeuralStock.Tools;


namespace NeuralStock.DomainService.Implementation
{
    public class MacroDataImportService : BaseService, IMacroDataImportService
    {
        private readonly IMacroDataService macroDataService;

        private int macroDataId;        
        
        public MacroDataImportService(IMacroDataService macroDataService)
        {
            this.macroDataService = macroDataService;
        }

        public void ImportData()
        {
            if (Directory.Exists(DirectoryRepository.MacroDataDirectory))
            {
                foreach (string filePath in Directory.GetFiles(DirectoryRepository.MacroDataDirectory))
                {
                    string fileName = Path.GetFileNameWithoutExtension(filePath);
                    if (Enum.IsDefined(typeof(MacroDataName), fileName))
                    {
                        IEnumerable<MacroDataData> results = ReadData(fileName);
                        if (results != null)
                        {                            
                            SaveData(results);
                        }
                    }
                }
            }
        }

        private IEnumerable<MacroDataData> ReadData(string fileName)
        {
            IEnumerable<MacroDataData> results;

            MacroDataName importedMacroData = (MacroDataName)Enum.Parse(typeof(MacroDataName), fileName);
            
            if (fileName == MacroDataName.AverageEmploymentInTheEnterpriseSector.ToString())
            {                
                results = ImportMacroDataFromCsv(importedMacroData, MacroDataType.Bankier);
            }
            else
            {
                results = ImportMacroDataFromCsv(importedMacroData, MacroDataType.Stooq);
            }

            return results;
        }

        private void SaveData(IEnumerable<MacroDataData> results)
        {
            foreach (var macroDataQuotation in results)
            {
                if (macroDataQuotation.IsValid())
                {
                    if (macroDataService.GetMacroDataQuotation(macroDataId, macroDataQuotation.Year.Value, macroDataQuotation.Month.Value) == null)
                    {
                        macroDataService.CreateQuotation(macroDataId, macroDataQuotation);
                    }
                    else
                    {
                        macroDataService.UpdateQuotation(macroDataId, macroDataQuotation);
                    }
                }
            }
        }

        private IEnumerable<MacroDataData> ImportMacroDataFromCsv(MacroDataName macroDataName, MacroDataType macroDataType)
        {
            MacroData macroData = macroDataService.GetMacroData(macroDataName.ToString());
            IEnumerable<MacroDataData> results = null;
            if (macroData != null)
            {
                macroDataId = macroData.Id;

                CsvReader<MacroDataData> csvReader = GetMacroDataCsvReader(macroDataType);
                
                string filePath = Path.Combine(DirectoryRepository.MacroDataDirectory, (macroDataName.ToString() + ".csv"));

                results = csvReader.GetDataFromCsv(filePath);
            }
            return results;
        }

        private CsvReader<MacroDataData> GetMacroDataCsvReader(MacroDataType macroDataType)
        {
            CsvReader<MacroDataData> csvReader;
            if (macroDataType == MacroDataType.Bankier)
            {
                csvReader = new CsvReader<MacroDataData>(new MacroDataBankierData());
            }
            else if (macroDataType == MacroDataType.Stooq)
            {
                csvReader = new CsvReader<MacroDataData>(new MacroDataStooqData());
            }
            else
            {
                throw new InvalidOperationException("Type of MacroData is not valid.");
            }
            return csvReader;
        }
    }
}
